<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${XxxDao}">

    <resultMap id="${XxxBean}Result" type="${XxxBean}">
    <#list model.properties as property>
        <result property="${property.propertyName}" column="${property.columnName}"/>
    </#list>
    </resultMap>

    <insert id="insert" parameterType="${XxxBean}">
        insert into `${tableName}` (
            <trim prefix="" suffixOverrides=",">
           <#list model.properties as property>
               <#if property.columnName!=id_col>
                   <if test="${property.propertyName}!=null">`${property.columnName}`,</if>
               </#if>
           </#list>
           </trim>
        )values(
           <trim prefix="" suffixOverrides=",">
           <#list model.properties as property>
               <#if property.columnName!=id_col>
                   <if test="${property.propertyName}!=null">${"#{"+property.propertyName+"}"},</if>
               </#if>
           </#list>
           </trim>
        )
        <selectKey resultType="${DataTypeAdapt.convert(model.keyProperty.clazzType)}" keyProperty="${id}">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <update id="update" parameterType="${XxxBean}">
        UPDATE `${tableName}` SET
         <#list model.properties as property><#if property.columnName==id_col><#assign id_index=property_index/></#if><#if property.propertyName!=id><#if property_index!=0 && id_index!=property_index-1>,</#if>`${property.columnName}`=${"#{"+property.propertyName+"}"}</#if></#list>
        WHERE `${id}`=${"#{"+id+"}"}
    </update>

    <update id="deleteBy${Id}" parameterType="${DataTypeAdapt.convert(model.keyProperty.clazzType)}">
        <![CDATA[
        delete from `${tableName}`
        where `${id}`=${"#{"+id+"}"}
        ]]>
    </update>

    <select id="selectBy${Id}" parameterType="${DataTypeAdapt.convert(model.keyProperty.clazzType)}"
            resultMap="${XxxBean}Result">
        SELECT * FROM `${tableName}` WHERE `${id}`=${"#{"+id+"}"} LIMIT 1
    </select>

    <select id="selectCount" parameterType="${XxxBean}" resultType="Int">
        SELECT COUNT(*) FROM `${tableName}` WHERE
        <trim prefix="where" prefixOverrides="and|or">
            <#list model.properties as property>
                <#if property.columnName!=id_col>
                    <if test="${property.propertyName}!=null">and `${property.columnName}` = ${"#{"+property.propertyName+"}"},</if>
                </#if>
            </#list>
        </trim>
    </select>

    <select id="selectList" parameterType="java.util.Map" resultType="${XxxBean}">
        SELECT * FROM `${tableName}` WHERE
        <trim prefix="where" prefixOverrides="and|or">
        <#list model.properties as property>
            <#if property.columnName!=id_col>
                <if test="${property.propertyName}!=null">and `${property.columnName}` = ${"#{"+property.propertyName+"}"},</if>
            </#if>
        </#list>
        </trim>
        LIMIT #startIndex, #limit
    </select>

</mapper>
